package com.ujoku.controller;

import com.labillusion.core.platform.exception.LoginRequiredException;
import com.labillusion.core.platform.exception.ResourceNotFoundException;
import com.labillusion.core.platform.web.pagination.PaginationForm;
import com.labillusion.core.platform.web.rest.RESTController;
import com.labillusion.core.util.MessageSourceUtils;
import com.ujoku.context.ActionContext;
import com.ujoku.domain.Favorite;
import com.ujoku.domain.Goods;
import com.ujoku.domain.Member;
import com.ujoku.interceptor.LoginRequired;
import com.ujoku.request.AddToFavForm;
import com.ujoku.service.CdnService;
import com.ujoku.service.FavoriteService;
import com.ujoku.service.GoodsService;
import com.ujoku.service.MemberService;
import com.ujoku.view.builder.GoodsViewBuilder;
import com.ujoku.view.domain.FavoriteView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Andy.Lian on 2015/1/28.
 */
@RestController
public class FavoriteController extends RESTController {
    @Autowired
    public FavoriteService favoriteService;
    @Autowired
    public GoodsService goodsService;
    @Autowired
    private GoodsViewBuilder goodsViewBuilder;
    @Autowired
    private ActionContext actionContext;
    @Autowired
    protected MessageSourceUtils messageSourceUtils;
    @Autowired
    protected CdnService cdnService;
    @Autowired
    public MemberService memberService;

    @RequestMapping(value="/favorite", method = RequestMethod.POST)
    @ResponseBody
    @LoginRequired
    public FavoriteView favoriteList(@RequestBody @Valid PaginationForm form) throws Exception{
        Member member = actionContext.getMember();
        if(member==null){
            throw new LoginRequiredException(messageSourceUtils.getMessage("session.login.required"));
        }
        Map<String,Object> query =new HashMap<>();
        query.put("user_id",member.getUser_id());
        int count=favoriteService.selectCount(query);
        query = form.getQueryMap();
        query.put("user_id",member.getUser_id());
        List<Favorite> result=favoriteService.selectList(query);
        for (Favorite fav:result){
            fav.setGoods_image(cdnService.buildGoodsImage(fav.getGoods_id(), CdnService.Goods_Small, fav.getUpdateTime().getTime()));
            fav.setVipPrice(fav.getPrice()*member.getDiscount());
        }
        FavoriteView view=new FavoriteView();
        view.setFavoriteList(result);
        view.setCount(count);
        return view;
    }

    @RequestMapping(value="/favorite/add", method = RequestMethod.POST)
    @ResponseBody
    @LoginRequired
    public FavoriteView add(@Valid @RequestBody AddToFavForm form){

        Member member = actionContext.getMember();
        if(member==null){
            throw new LoginRequiredException(messageSourceUtils.getMessage("session.login.required"));
        }
        Favorite fav=favoriteService.getFavoriteGoodsByUserId(member.getUser_id(),form.getGoods_id());
        if(fav!=null){
            throw new ResourceNotFoundException(messageSourceUtils.getMessage("goods.has.favorite"));
        }
        Goods goods = goodsService.selectById(form.getGoods_id());
        Favorite favorite=new Favorite();
        favorite.setGoods_id(goods.getGoods_id());
        favorite.setGoods_name(goods.getGoods_name());
        favorite.setPrice(goods.getPrice());
        favorite.setUser_id(member.getUser_id());
        if(favoriteService.insert(favorite)>0) {
            List<Favorite> result = favoriteService.getFavoritesByUserId(member.getUser_id());
            member.setFavCount(result.size());
            memberService.update(member);
            actionContext.setMember(member);
            FavoriteView view = new FavoriteView();
            view.setFavoriteList(result);
            return view;
        }
        throw new ResourceNotFoundException(messageSourceUtils.getMessage("add.favorite.failed"));
    }

    @RequestMapping(value="/favorite/remove/{goodsId}", method = RequestMethod.DELETE)
    @ResponseBody
    public FavoriteView remove(@PathVariable String goodsId){
        Member member = actionContext.getMember();
        if(member==null){
            throw new LoginRequiredException(messageSourceUtils.getMessage("session.login.required"));
        }
        if(favoriteService.deleteFavoriteByGoodsId(member.getUser_id(),goodsId)>0) {
            List<Favorite> result = favoriteService.getFavoritesByUserId(member.getUser_id());
            member.setFavCount(result.size());
            memberService.update(member);
            actionContext.setMember(member);
            FavoriteView view = new FavoriteView();
            view.setFavoriteList(result);
            return view;
        }
        throw new ResourceNotFoundException(messageSourceUtils.getMessage("favorite.not.found"));
    }
}
